home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / New System Software Extensions / QuickDraw™ GX 1.1.2 / Programming Stuff / Sample Code / Printing Samples / Printer Drivers… / CustomWriter GX 1.0.3 ƒ / NewApp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-20  |  7.1 KB  |  232 lines  |  [TEXT/MPS ]

  1. /*
  2.     FILENAME
  3.         NewApp.c
  4.  
  5.     DESCRIPTION
  6.         Contains code for all of our message overrides except the old-application
  7.         compatibility, custom I/O, and custom buffering ones.
  8.  
  9.     COPYRIGHT
  10.         Copyright © 1995 Apple Computer, Inc.
  11.         All rights reserved.
  12.     
  13.     Modification history
  14.         06/14/95 - Dave Hersey -    Version 1.0.3 to fix a bug in
  15.                                     CustomBufferingAndIO.c when creating
  16.                                     high-res PICTs, and to make the size
  17.                                     of buffers more flexible.
  18.  
  19.         05/26/95 - Dave Hersey -    Version 1.0.2 to add the new 'outp'
  20.                                     desktop printer resource so that
  21.                                     paper mismatch and manual feed
  22.                                     alerts are suppressed.  See the
  23.                                     SD_DefaultDesktopPrinter routine
  24.                                     for details.
  25.  
  26.         05/03/95 - Dave Hersey -    Version 1.0.1 to fix some minor bugs in
  27.                                     CustomBufferingAndIO.c.
  28.  
  29.         01/14/95 - Dave Hersey -    Created from the shell of a hollowed-out
  30.                                     ImageWriter driver.
  31.  
  32.     NOTE: Relevant goodies are listed in MPW's "Mark" menu.
  33.  
  34. */
  35.  
  36. #include "CommonDefines.h"
  37.  
  38.  
  39. /*    -----------------------------------------------------------------------
  40.  
  41.     SD_RasterDataIn is an override of gxRasterDataIn.  It simply takes the
  42.     bitmap data passed to us, and sends it to the "device" using
  43.     Send_GXBufferData.  For this driver, we're being passed 32-bit XRGB
  44.     data (where "X" indicates an unused alpha byte), stripping off the
  45.     unused byte, and sending it to gxBufferData as RGB data.
  46.     
  47.     If we find that we've moved over blank lines, we call WriteBlankLines
  48.     to send RGB bands of white to gxBufferData.  If your device supports
  49.     it, it is far more economical to skip the white space, rather than
  50.     image it.
  51.  
  52.     -----------------------------------------------------------------------    */
  53.  
  54. OSErr SD_RasterDataIn (gxOffscreenHdl hOffscreen, gxRectangle *bandRect, gxRectangle *dirtyRect)
  55. {
  56.     OSErr                anErr;
  57.     char                lockState;
  58.     gxBitmap            *pBitmap;
  59.     unsigned char        *XRGBDataPtr, *RGBDataPtr, *scanStart;
  60.     long                rowNum, colNum, amtToWrite, numBlankLinesToDo;
  61.     Ptr                    baseAddr;
  62.     DriverGlobalsHdl    drvrGlobalsHdl = GetDriverGlobals();
  63.  
  64. #pragma unused(dirtyRect);
  65.  
  66.     // Update the status display to indicate that we're sending data
  67.     // to the "printer."        
  68.  
  69.     anErr = GXReportStatus(kTransmissionStatID, kSendingPartStatIdx);
  70.     require(anErr == noErr, ReportStatus_Failed);
  71.  
  72.  
  73.     // Write some blank lines if we need to. 
  74.  
  75.     numBlankLinesToDo = (bandRect->top >>16) - (*drvrGlobalsHdl)->lastYPos;
  76.     
  77.     if (numBlankLinesToDo > 0)
  78.     {
  79.         anErr = WriteBlankLines(numBlankLinesToDo, bandRect);
  80.         nrequire(anErr, WriteBlankLines_Failed);
  81.     }
  82.  
  83.     // Make sure the offscreen bitmap is locked down, and then repackage the
  84.     // data.  To save space, we're converting the XRGB data to RGB data in
  85.     // the same imaging buffer that was passed to us.
  86.  
  87.     lockState = HGetState((Handle) hOffscreen);
  88.     HLockHi((Handle) hOffscreen);
  89.  
  90.     pBitmap = &(*hOffscreen)->thePlanes[0].theBits;
  91.     RGBDataPtr = XRGBDataPtr = (unsigned char *) ((unsigned long) pBitmap->image + (unsigned long) ((bandRect->left >>16) * 4));
  92.     baseAddr = (Ptr) RGBDataPtr;
  93.  
  94.  
  95.     // Convert all data in this raster bitmap to 24-bit RGB.  XRGBData
  96.     // points to the next byte of unconverted data, and RGBDataPtr
  97.     // points to the next place to store the converted RGB data.
  98.  
  99.     for (rowNum = (bandRect->top >>16); rowNum < (bandRect->bottom >>16); rowNum++)
  100.     {
  101.         scanStart = XRGBDataPtr;
  102.         
  103.         for (colNum = (bandRect->left >>16); colNum < (bandRect->right >>16); colNum++)
  104.         {
  105.             ++XRGBDataPtr;                // skip those dang alphas!
  106.             *RGBDataPtr = *XRGBDataPtr;    // copy Red component
  107.             ++RGBDataPtr;
  108.             ++XRGBDataPtr;
  109.             *RGBDataPtr = *XRGBDataPtr;    // copy Green component
  110.             ++RGBDataPtr;
  111.             ++XRGBDataPtr;
  112.             *RGBDataPtr = *XRGBDataPtr;    // copy Blue component
  113.             ++RGBDataPtr;
  114.             ++XRGBDataPtr;
  115.         }
  116.             
  117.         if (rowNum == (bandRect->top >>16))    // 1st row?  Store some things for later.
  118.         {
  119.             (*drvrGlobalsHdl)->pixMapRowBytes = (long) XRGBDataPtr - (long) baseAddr;
  120.             (*drvrGlobalsHdl)->pixMapBounds.bottom += (bandRect->bottom  >>16) - (bandRect->top >>16);
  121.             (*drvrGlobalsHdl)->pixMapBounds.right = (bandRect->right >>16) - (bandRect->left >>16);
  122.         }
  123.  
  124.         // Bump XRGBDataPtr to the start of the next scan line.
  125.         XRGBDataPtr = scanStart + (pBitmap->rowBytes & 0x7FFF);
  126.     }
  127.  
  128.     // Write out the data for this band.
  129.  
  130.     amtToWrite = (long) RGBDataPtr - (long) baseAddr;
  131.     anErr = Send_GXBufferData(baseAddr, amtToWrite, gxNoBufferOptions);
  132.  
  133.     // Reset the handle's lock on the way out, and update our
  134.     // lastYPos variable.
  135.  
  136.     HSetState((Handle) hOffscreen, lockState);
  137.  
  138. WriteBlankLines_Failed:
  139. ReportStatus_Failed:
  140.     (*drvrGlobalsHdl)->lastYPos = (bandRect->bottom >>16) +1;
  141.  
  142.     return anErr;
  143. }
  144.  
  145.  
  146. /*    -----------------------------------------------------------------------
  147.  
  148.     WriteBlankLines is a routine used to add blank lines to our output.
  149.     We use this to add "skipped over" lines to our output from
  150.     SD_RasterDataIn.  Since we're printing to a file, we need to add the
  151.     white space as bands of white data.
  152.  
  153.     -----------------------------------------------------------------------    */
  154.  
  155. OSErr WriteBlankLines(long numBlankLinesToDo, gxRectangle *bandRect) 
  156. {
  157.     OSErr                anErr = noErr;
  158.     long                line, aByte, lineLength;
  159.     unsigned char        *blankLine, *whichByte;
  160.  
  161.     // Record some "blank lines" in our file.  NOTE: This assumes we output 24-bit RGB data.
  162.  
  163.     if (numBlankLinesToDo > 0)
  164.     {
  165.         // Create a pointer to hold one white (0xFF, 0xFF, 0xFF…) line.
  166.  
  167.         lineLength = 3 * ((bandRect->right >>16) - (bandRect->left >> 16));
  168.         blankLine = (unsigned char *) NewPtrSys(lineLength);
  169.         anErr = MemError();
  170.         nrequire(anErr, NewPtrSys_Failed);
  171.  
  172.         // Make it white.
  173.  
  174.         for (aByte = 0, whichByte = blankLine; aByte < lineLength; aByte++, whichByte++)
  175.             *whichByte = 0xFF;
  176.  
  177.         // Write it to the file as many times as necessary, then dispose of our pointer.
  178.  
  179.         for (line = 0; line < numBlankLinesToDo; line++)
  180.             anErr = Send_GXBufferData((Ptr) blankLine, lineLength, gxNoBufferOptions);
  181.  
  182.         DisposePtr((Ptr) blankLine);
  183.     }
  184.  
  185. NewPtrSys_Failed:
  186.     return anErr; 
  187. }
  188.  
  189.  
  190.  
  191. /*    -----------------------------------------------------------------------
  192.  
  193.     SD_DefaultDesktopPrinter is an override of gxDefaultDesktopPrinter.
  194.     It adds an 'outp' resource to the newly created desktop printer.
  195.     This resource was introduced for QuickDraw GX 1.1, and allows us to
  196.     specify that we don't have paper trays, so paper mismatches and manual
  197.     feed alerts should be suppressed.
  198.  
  199.     -----------------------------------------------------------------------    */
  200.  
  201. OSErr SD_DefaultDesktopPrinter(Str31 dtpName)
  202. {
  203.     OSErr                        anErr;
  204.     GXDriverOutputSettingsHdl    resHdl;
  205.     
  206. //    Forward the message, so the desktop printer creation is completed.
  207.  
  208.     anErr = Forward_GXDefaultDesktopPrinter(dtpName);
  209.     nrequire(anErr, Failed_DefaultDesktopPrinter);
  210.  
  211.  
  212. //    Create a GXDriverOutputSettingsHdl that has the outputSettings
  213. //    field cleared, rather than set to gxCanConfigureTrays.
  214.  
  215.     resHdl = (GXDriverOutputSettingsHdl) NewHandleSysClear(sizeof(GXDriverOutputSettings));
  216.     require(resHdl, Failed_NewHandleSysClear);
  217.  
  218.  
  219. //    Add the handle to our desktop printer, and then dispose of it.
  220.  
  221.     anErr = GXWriteDTPData(dtpName, gxDriverOutputType, gxDriverOutputTypeID, (Handle) resHdl);
  222.     nrequire(anErr, Failed_WriteDTPResource);
  223.     
  224.     DisposeHandle((Handle) resHdl);
  225.     
  226. Failed_WriteDTPResource:
  227. Failed_NewHandleSysClear:
  228. Failed_DefaultDesktopPrinter:
  229.  
  230.     return anErr;
  231. }
  232.